package com.muyu.system.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.system.domain.DanmoVipPackage;
import com.muyu.system.mapper.DanmoVipPackageMapper;
import com.muyu.system.service.IDanmoVipPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * vip套餐包Service业务层处理
 * 
 * @author yl
 * @date 2025-06-10
 */
@Service
public class DanmoVipPackageServiceImpl implements IDanmoVipPackageService
{
    @Autowired
    private DanmoVipPackageMapper danmoVipPackageMapper;

    /**
     * 查询vip套餐包
     * 
     * @param id vip套餐包主键
     * @return vip套餐包
     */
    @Override
    public DanmoVipPackage selectDanmoVipPackageById(Long id)
    {
        return danmoVipPackageMapper.selectDanmoVipPackageById(id);
    }

    /**
     * 查询vip套餐包列表
     * 
     * @param danmoVipPackage vip套餐包
     * @return vip套餐包
     */
    @Override
    public List<DanmoVipPackage> selectDanmoVipPackageList(DanmoVipPackage danmoVipPackage)
    {
        return danmoVipPackageMapper.selectDanmoVipPackageList(danmoVipPackage);
    }

    /**
     * 新增vip套餐包
     * 
     * @param danmoVipPackage vip套餐包
     * @return 结果
     */
    @Override
    public int insertDanmoVipPackage(DanmoVipPackage danmoVipPackage)
    {
        danmoVipPackage.setCreateTime(DateUtils.getNowDate());
        return danmoVipPackageMapper.insertDanmoVipPackage(danmoVipPackage);
    }

    /**
     * 修改vip套餐包
     * 
     * @param danmoVipPackage vip套餐包
     * @return 结果
     */
    @Override
    public int updateDanmoVipPackage(DanmoVipPackage danmoVipPackage)
    {
        danmoVipPackage.setUpdateTime(DateUtils.getNowDate());
        return danmoVipPackageMapper.updateDanmoVipPackage(danmoVipPackage);
    }

    /**
     * 批量删除vip套餐包
     * 
     * @param ids 需要删除的vip套餐包主键
     * @return 结果
     */
    @Override
    public int deleteDanmoVipPackageByIds(Long[] ids)
    {
        return danmoVipPackageMapper.deleteDanmoVipPackageByIds(ids);
    }

    /**
     * 删除vip套餐包信息
     * 
     * @param id vip套餐包主键
     * @return 结果
     */
    @Override
    public int deleteDanmoVipPackageById(Long id)
    {
        return danmoVipPackageMapper.deleteDanmoVipPackageById(id);
    }

    @Override
    public DanmoVipPackage packages(DanmoVipPackage danmoVipPackage) {
        return danmoVipPackageMapper.packages(danmoVipPackage);
    }

    @Override
    public Result<Map<String, Object>> packagesList(DanmoVipPackage danmoVipPackage) {
        List<DanmoVipPackage> danmoVipPackages = danmoVipPackageMapper.packagesList(danmoVipPackage);
        Map<String, Object> result = new HashMap<>();

        // 提取年费会员
        DanmoVipPackage annualPackage = danmoVipPackages.stream()
                .filter(packageItem -> "年费会员".equals(packageItem.getName()))
                .findFirst()
                .orElse(null);

        // 过滤其他会员
        List<DanmoVipPackage> otherPackages = danmoVipPackages.stream()
                .filter(packageItem -> !"年费会员".equals(packageItem.getName()))
                .collect(Collectors.toList());

        result.put("annualPackage", annualPackage);
        result.put("otherPackages", otherPackages);
        return Result.success(result);

    }
}
